package cn.cctvcc.system.service.impl;

import cn.cctvcc.core.enums.common.CommonEnum;
import cn.cctvcc.core.utils.SnowIdUtils;
import cn.cctvcc.core.web.page.MybatisPlusPageUtil;
import cn.cctvcc.system.api.domain.SysUser;
import cn.cctvcc.system.mapper.SysUserMapper;
import cn.cctvcc.system.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @description: 用户信息实现类
 * @author: Jiang
 * @create: 2021-09-22 15:59
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 列表查询
     * @param sysUser
     * @return
     */
    @Override
    public List<SysUser> selectPage(SysUser sysUser) {
        Page userPage = MybatisPlusPageUtil.startPage();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sysUser);
        queryWrapper.lambda().orderByDesc(SysUser::getUserId);
        return  sysUserMapper.selectPage(userPage, queryWrapper).getRecords();
    }

    /**
     * 新增用户信息
     *
     * @param sysUser
     * @return
     */
    @Override
    public SysUser add(SysUser sysUser, Boolean isRegister) {
        // isRegister: true 注册 false 新增
        if (Objects.isNull(isRegister) || Objects.equals(false, isRegister)) {

        } else {
            sysUser.setDeptId(0l);
            sysUser.setUserType("1");
        }
        sysUser.setUserId(SnowIdUtils.uniqueLong());
        sysUser.setCreateBy("");
        sysUser.setCreateTime(LocalDateTime.now());
        sysUserMapper.insert(sysUser);
        return sysUser;
    }

    /**
     * 修改用户信息
     *
     * @param sysUser
     * @return
     */
    @Override
    public int update(SysUser sysUser) {
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysUser::getUserId, sysUser.getUserId());
        sysUser.setUpdateTime(LocalDateTime.now());
        return sysUserMapper.update(sysUser, updateWrapper);
    }

    /**
     * 根据用户id获取用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public SysUser selectUserByUserId(Long userId) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getUserId, userId);
        return sysUserMapper.selectOne(queryWrapper);
    }

    /**
     * 根据用户名称获取用户
     *
     * @param userName
     * @return
     */
    @Override
    public SysUser selectUserByUserName(String userName) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getUserName, userName);
        return sysUserMapper.selectOne(queryWrapper);
    }

    /**
     * 检查用户名是否唯一
     *
     * @param sysUser
     * @return
     */
    @Override
    public String checkUserNameUnique(SysUser sysUser) {
        Long userId = Objects.isNull(sysUser.getUserId()) ? -1L : sysUser.getUserId();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getUserName, sysUser.getUserName());
        sysUser = sysUserMapper.selectOne(queryWrapper);
        return checkUser(sysUser, userId);
    }

    /**
     * 检查邮箱是否唯一
     *
     * @param sysUser
     * @return
     */
    @Override
    public String checkEmailUnique(SysUser sysUser) {
        Long userId = Objects.isNull(sysUser.getUserId()) ? -1L : sysUser.getUserId();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getEmail, sysUser.getEmail());
        sysUser = sysUserMapper.selectOne(queryWrapper);
        return checkUser(sysUser, userId);
    }

    private String checkUser(SysUser sysUser, Long userId) {
        if (Objects.isNull(sysUser) || Objects.equals(userId, sysUser.getUserId()))
            return CommonEnum.YES.getCode();
        return CommonEnum.NO.getCode();
    }

}
